package org.goodsl.mdd;

import groovy.lang.Closure;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Simon Xiao
 * Date: 13-1-18
 * Time: 上午8:50
 * Copyright goodsl.org 2012~2020
 */
public class SegInBox {
    public Class boxSegType;
    public String segName;
    public boolean isBox;
    public SegInfo segInfo;

    protected TypeSetting setting;
    public TypeSetting getSetting(){
        return setting;
    }

    public HashMap<String, ArrayList<SegInBox>> dependOn = new HashMap<String, ArrayList<SegInBox>>();
    public HashMap<String, ArrayList<SegInBox>> dependBy = new HashMap<String, ArrayList<SegInBox>>();

    /**
     *   保存依赖属性的闭包，key 为依赖属性的名称
     */
    public HashMap<String, Closure> dependSegCode = new HashMap<String, Closure>(4);


    public SegInBox(Class cz, SegInfo segInfo, String segName) {
        this(cz, segInfo, segName, false);
    }

    public SegInBox(Class cz, SegInfo segInfo, String segName, boolean box) {
        boxSegType = cz;
        this.segName = segName;
        isBox = box;
        this.segInfo = segInfo;
    }

    public boolean hasDependSeg(String name) {
        return dependSegCode.containsKey(name);
    }

    public void dependOn(String dependAspectName, SegInBox other) {
        dependOn(dependAspectName, other, false); //默认不回绕

    }

    public void dependOn(String dependAspectName, SegInBox other, boolean loop) {
        ArrayList<SegInBox> list = dependOn.get(dependAspectName);
        if (list == null) {
            list = new ArrayList<SegInBox>();
            dependOn.put(dependAspectName, list);
        }
        list.add(other);
        if (loop)
            other.dependBy(dependAspectName, this, false);
    }

    public void dependBy(String dependAspectName, SegInBox other) {
        dependBy(dependAspectName, other, false); //默认不回绕

    }

    public void dependBy(String dependAspectName, SegInBox other, boolean loop) {
        ArrayList<SegInBox> list = dependBy.get(dependAspectName);
        if (list == null) {
            list = new ArrayList<SegInBox>();
            dependBy.put(dependAspectName, list);
        }
        list.add(other);
        if (loop)
            other.dependOn(dependAspectName, this, false);
    }

    public void setSegValue(Box box){

    }
}
